package com.yxy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.yxy.domain.dto.ResourcesDTO;
import com.yxy.domain.po.Resources;
import com.yxy.domain.vo.ResourcesVO;
import com.yxy.mapper.ResourcesMapper;
import com.yxy.service.ResourcesService;
import com.yxy.utils.QiniuOSSUtil;
import com.yxy.utils.SplitUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ResourcesServiceImpl implements ResourcesService {

    @Autowired
    private ResourcesMapper resourcesMapper;

    /**
     * 查询资源信息
     * @return
     */
    @Override
    public ResourcesVO list() {
        Resources resources = resourcesMapper.selectAll();
        ResourcesVO resourcesVO = BeanUtil.copyProperties(resources, ResourcesVO.class);
        return resourcesVO;
    }

    /**
     * 修改资源信息
     * @param resourcesDTO
     */
    @Override
    public void update(ResourcesDTO resourcesDTO) {
        Resources resources = BeanUtil.copyProperties(resourcesDTO, Resources.class);
        resourcesMapper.update(resources);
        if(resourcesDTO.getOriginal() != null){
            QiniuOSSUtil.deleteFileFromQiniu("sports/" + SplitUtil.split(resourcesDTO.getOriginal()));
        }
    }
}
